package org.gui;

import java.util.Vector;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

import org.mysql.MySQL;


public class TaskTable implements TableModel {

	/**
	 * Vector of all tasks
	 */
	private Vector<Task> tasks = new Vector<Task>();
	
	/**
	 * Vector of all listeners
	 */
	private Vector<TableModelListener> listeners = new Vector<TableModelListener>();
	
	/**
	 * Columns
	 */
	private String[] columns = { "ID", "Status", "Zeit" };

	/**
	 * Adding a task
	 */
	public void addTask(Task task)
	{
		// Task Index
		int index = tasks.size();
		
		// Adding task to vector
		tasks.add(task);

		TableModelEvent e = new TableModelEvent( this, index, index, 
                TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT );
		
		for( int i = 0, n = listeners.size(); i<n; i++ ){
            ((TableModelListener)listeners.get( i )).tableChanged( e );
        }
	}
	
	/**
	 * Remove all tasks
	 */
	public void removeAllTasks()
	{
		// Task Index
		int index = tasks.size();
		
		// Set vector to null
		tasks.removeAllElements();
		
		TableModelEvent e = new TableModelEvent(this, 0, index,
				TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE);
		
		for( int i = 0, n = listeners.size(); i<n; i++ ){
            ((TableModelListener)listeners.get( i )).tableChanged( e );
        }
	}
	
	/**
	 * Add listener to vector
	 */
	public void addTableModelListener(TableModelListener l) {
		listeners.add(l);
	}

	/**
	 * Returns the column class
	 */
	public Class<?> getColumnClass(int columnIndex) {
        switch( columnIndex ){
            case 0: return Integer.class;
            case 1: return String.class;
            case 2: return String.class;
            default: return null;
        }   
    }

	/**
	 * Get column-count
	 */
	public int getColumnCount() {
		return columns.length;
	}

	/**
	 * Get column name by column index
	 */
	public String getColumnName(int column) {
		if(column < 0 || column > columns.length) return null;
		return columns[column];
	}

	/**
	 * Get row-count
	 */
	public int getRowCount() {
		return tasks.size();
	}

	/**
	 * Returns the value of a single cell
	 */
	public Object getValueAt(int rowIndex, int columnIndex) {
		// Get row
        Task task = (Task)tasks.get( rowIndex );
        
        // Get Column and return content of row/column
        switch( columnIndex ){
            case 0: return task.getId();
            case 1: return task.getStatus();
            case 2: return task.getTime();
            default: return null;
        }
    }

	/**
	 * 
	 */
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}

	/**
	 * Remove listener from vector
	 */
	public void removeTableModelListener(TableModelListener l) {
		listeners.remove(l);
		
	}

	/**
	 * 
	 */
	public void setValueAt(Object arg0, int arg1, int arg2) {
		// ...
	}
	
	/**
	 * Resets the table, loads tasks from DB and adds those to the table
	 */
	public void loadAllTasksFromDB()
	{
		// Remove current data from table
		this.removeAllTasks();
		
		// MySQL-Instance
		MySQL conn = MySQL.getInstance();
		
		// Query
		conn.query(
				"SELECT " +
				    "a.id, " +
				    "s.value, " +
				    "a.timestamp " +
				"FROM " +
				    "tbl_auftrag AS a " + 
				"INNER JOIN " +
				    "tbl_auftragstatus AS s " +
				"ON " +
				    "a.status = s.idstatus " +
				"ORDER BY " +
					"a.status DESC, " +
				    "a.timestamp ASC"
		);
		
		// Table and Row Vector for fetching the row data
		Vector< Vector <String> > table = conn.resultSetToVector();
		Vector<String> row = new Vector<String>();
		
		// Temporary for row data
		String[] rowData = new String[conn.getColumnCount()];
		
		// Iterate through rows
		for(int i=0; i<table.size(); i++) {
			row = table.get(i);
			
			// Iterate through columns
			for(int j=0; j<row.size(); j++) {
				// Save data
				rowData[j] = row.get(j);
				System.out.println(rowData[j]);
			}
			
			// Timestamp long value
			Long ts = new Long(rowData[2]);
			
			// Add task to vector
			tasks.add(new Task(
					new Integer(rowData[0]),
					rowData[1],
					ts.longValue()
					));
		}
	}

}
